package cn.group01.git.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.group01.git.common.CodeMsg;
import cn.group01.git.common.ResponseResult;
import cn.group01.git.dto.AgentDTO;
import cn.group01.git.entity.Agent;
import cn.group01.git.entity.Goods;
import cn.group01.git.mapper.AgentMapper;
import cn.group01.git.service.AgentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @Author: 加瓦程序员小鹏
 * @Date: 2023/7/11
 * @Description: 代理商业务层实现类
 */

@Service
public class AgentServiceImpl implements AgentService {

    @Autowired
    private AgentMapper agentMapper;


    public ResponseResult getList(AgentDTO agentDTO) {

        PageHelper.startPage(agentDTO.getPage(),agentDTO.getLimit());

        List<Agent> agents = agentMapper.getPage(agentDTO);

        PageInfo<Agent> agentPageInfo = new PageInfo<Agent>(agents);

        List<Agent> list = agentPageInfo.getList();
        long total = agentPageInfo.getTotal();

        return ResponseResult.success(CodeMsg.SUCCESS,list,total);
    }

    public List<Agent> selectList() {
        System.out.println(agentMapper.selectList());
        return agentMapper.selectList();
    }

    public ResponseResult batchAdd(MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseResult.fail(CodeMsg.EMPTY_FILE_ERROR);
        }
        //解析excel表格
        try {
            InputStream inputStream = file.getInputStream();
            //导入参数
            ImportParams importParams = new ImportParams();
            importParams.setTitleRows(1);
            importParams.setHeadRows(1);
            List<Agent> agents = ExcelImportUtil.importExcel(inputStream, Agent.class, importParams);
            for (Agent agent : agents) {
                String agentName = agent.getAgentName();
                Agent agt = agentMapper.getByAgentName(agentName);
                if (!ObjectUtils.isEmpty(agt)) {
                    throw new RuntimeException("代理商重复");
                }
                //添加代理商信息到数据库
                agentMapper.addAgent(agent);
            }
            return ResponseResult.success(CodeMsg.SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("服务器异常");
        }
    }

    public ResponseResult addAgent(Agent agent) {

        // 判定代理商是否存在
        Agent agent1 = agentMapper.getByAgentName(agent.getAgentName());
        if (!ObjectUtils.isEmpty(agent1)){
            return ResponseResult.fail(CodeMsg.AGENT_EXIST);
        }

        agentMapper.addAgent(agent);

        return ResponseResult.success(CodeMsg.SUCCESS);
    }


    public Agent getById(String agentId) {
        Agent agent = agentMapper.getById(agentId);
        // 测试数据
        System.out.println("agent = " + agent);
        return agent;
    }


    public ResponseResult editAgent(Agent agent) {

        agentMapper.editAgent(agent);
        return ResponseResult.success(CodeMsg.SUCCESS);
    }


    public ResponseResult delete(Integer[] ids) {
        agentMapper.delete(ids);

        return ResponseResult.success(CodeMsg.SUCCESS);
    }

}
